#!/bin/bash

# Vue3管理系统后端部署到Sealos脚本

echo "🚀 开始部署Vue3管理系统后端到Sealos..."

# 1. 构建Docker镜像
echo "📦 构建Docker镜像..."
docker build -t vue3-management-backend:latest .

if [ $? -ne 0 ]; then
    echo "❌ Docker镜像构建失败"
    exit 1
fi

echo "✅ Docker镜像构建成功"

# 2. 标记镜像（根据实际情况修改）
echo "🏷️  标记镜像..."
docker tag vue3-management-backend:latest sealos-registry.local/vue3-management-backend:latest

# 3. 推送镜像
echo "📤 推送镜像到Sealos镜像仓库..."
docker push sealos-registry.local/vue3-management-backend:latest

if [ $? -ne 0 ]; then
    echo "❌ 镜像推送失败"
    exit 1
fi

echo "✅ 镜像推送成功"

# 4. 部署到Kubernetes
echo "🚀 部署到Kubernetes..."
kubectl apply -f k8s-deployment.yaml

if [ $? -ne 0 ]; then
    echo "❌ Kubernetes部署失败"
    exit 1
fi

echo "✅ Kubernetes部署成功"

# 5. 等待部署完成
echo "⏳ 等待部署完成..."
kubectl wait --for=condition=available --timeout=300s deployment/vue3-management-backend

if [ $? -ne 0 ]; then
    echo "❌ 部署超时"
    exit 1
fi

echo "✅ 部署完成！"

# 6. 显示部署状态
echo "📊 部署状态："
kubectl get pods -l app=vue3-management-backend
kubectl get svc vue3-management-backend-service
kubectl get ingress vue3-management-backend-ingress

echo ""
echo "🎉 后端服务部署成功！"
echo "📝 访问地址："
echo "   - 健康检查: http://api.vue3-management.local/health"
echo "   - 登录接口: http://api.vue3-management.local/mock/token"
echo "   - 用户信息: http://api.vue3-management.local/mock/userinfo"
echo ""
echo "�� 记得修改前端配置以连接真实后端！"
